package com.i2works.smartluolong.utils.shiro.filter;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


/**
 * 用户退出拦截器
 *
 * @author koal <koal@vip.qq.com>
 * @date 2017/2/25 23:01
 */
public class UserLogoutFilter extends LogoutFilter {


    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        Subject subject = getSubject(request, response);
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String redirectUrl = httpServletRequest.getHeader("Referer");
        if (StringUtils.isBlank(redirectUrl)) {
            redirectUrl = getRedirectUrl(request, response, subject);
        } else if (redirectUrl.contains("user")) {
            redirectUrl = "/";
        }
        subject.logout();
        this.issueRedirect(request, response, redirectUrl);
        return false;
    }

}
